package com.kevin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.kevin.model.Article;
import com.kevin.model.ArticleInfo;
import com.kevin.service.ArticleInfoService;
import com.kevin.service.OutputService;
import com.kevin.validate.Errors;


/**
 * 
  * @ClassName: OutputAction 
  * @Description: 输出模块
  * @author 吕凯
  * @date 2012-10-29 上午10:24:17 
  *
 */
@IocBean
@At("/output")
public class OutputAction {	

	@Inject
	protected ArticleInfoService articleInfoService;
	@Inject
	protected OutputService outputService;
	
	/**
	 * 
	  * @方法名  outExcel 
	  * @功能描述  输出电子表格
	  * @author   吕凯  
	  * @param request
	  * @param response
	  * @param context
	  * @param es
	  * @throws Exception 
	  * @return  void 
	  * @编码时间 2012-10-29 上午10:03:52
	 */
	@At
	@Ok("raw")
	public void outExcel(@Param("t")String type,HttpServletRequest request,HttpServletResponse response, ServletContext context,Errors es) throws Exception{
		System.out.println(type);
		if(type == null)
			return;
		if(type.equals("article")){
			outputExcelArticle(request, response);
		}else if(type.equals("articleinfo")){
			outputExcelArticleInfo(request, response);
		}
		
		System.out.println("out===");
		
	}
	/**
	 * 
	  * @方法名  outputExcelArticleInfo 
	  * @功能描述  输出稿件
	  * @author   吕凯  
	  * @param request
	  * @param response
	  * @throws Exception 
	  * @return  void 
	  * @编码时间 2012-11-6 下午04:18:48
	 */
	private void outputExcelArticleInfo(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SqlExpressionGroup group = Cnd.exps("1", "=", "1");

		String title = request.getParameter("title");
		if (title != null && !title.equals("")) {
			group = group.and("title", "like", "%"+title + "%");
		}
		String addDate = request.getParameter("addDate");
		if (addDate != null && !addDate.equals("")) {
			group = group.and("addDate", "like", "%" + addDate + "%");
		}
		 
		 //查询内容
		List<ArticleInfo> dataList = articleInfoService.getArticleInfoList(request, Cnd.where(group).desc("addDate"));//数据源
		String excelName ="标题,发布者,发布日期 ";//标题 
		String[] excelKeyArray = {"title","addUser.name","addDate"};//取字段
//		String[] excelKeyArray = {"title","addUser.name","addDate"+FILEDCONCAT+"addDate"};//addDate%$-%$addDate 代表多字段连接 已
		 
		 
		 Map<Integer, Object> formatMap = new HashMap<Integer, Object>();
		 List<Object> paramList = new ArrayList<Object>();
		 paramList.add("yyyy年MM月dd日");
		 //输出转换
//		 Map parampMap = new HashMap();
//		 parampMap.put("男", "1");
//		 parampMap.put("女", "0");
		 formatMap.put(2, new Object[]{"date",paramList});//格式化 第二个属性 date格式
//		 formatMap.put(1, new Object[]{"switch",parampMap});//格式化 switch类型
		 
		 Boolean showNum = false;//是否显示序号
		 outputService.outputExcel(request, response,"年值班表",excelName,excelKeyArray,dataList,showNum,formatMap);
	}
	/**
	 * 
	  * @方法名  outputExcelArticle 
	  * @功能描述  输出稿件
	  * @author   吕凯  
	  * @param request
	  * @param response
	  * @throws Exception 
	  * @return  void 
	  * @编码时间 2012-11-6 下午04:19:01
	 */
	private void outputExcelArticle(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SqlExpressionGroup group = Cnd.exps("1", "=", "1");

		String title = request.getParameter("title");
		if (title != null && !title.equals("")) {
			group = group.and("title", "like", "%"+title + "%");
		}
		String addDate = request.getParameter("addDate");
		if (addDate != null && !addDate.equals("")) {
			group = group.and("addDate", "like", "%" + addDate + "%");
		}
		 
		 //查询内容
		List<Article> dataList = articleInfoService.getArticleList(request, Cnd.where(group).desc("modifyDate"));//数据源
		String excelName ="标题,发布者,发布日期 ";//标题 
		String[] excelKeyArray = {"title","addUser.name","modifyDate"};//取字段
		 
		 
		 Map<Integer, Object> formatMap = new HashMap<Integer, Object>();
		 List<Object> paramList = new ArrayList<Object>();
		 paramList.add("yyyy年MM月dd日");
		 //输出转换
		 formatMap.put(2, new Object[]{"date",paramList});//格式化 第二个属性 date格式
		 
		 Boolean showNum = true;//是否显示序号
		 outputService.outputExcel(request, response,"稿件列表",excelName,excelKeyArray,dataList,showNum,formatMap);
	}
	
	
}
